package com.hotel.util;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet基类
 * 丰哥
 * 2019年7月8日
 *
 */
public class BaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public BaseServlet() {
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		//获得请求的动作
		String action = request.getParameter("action");
		
		//aciton，实际就是要调用方法的方法名		
		if (action == null || action.trim().equals("")) {//当未指定action的时候
			action = "execute";
		}
		
		//通过反射机制，执行指定方法
		Class<? extends BaseServlet> cls = getClass();
		try {
			Method method = cls.getMethod(action, HttpServletRequest.class, HttpServletResponse.class);
			String url = (String) method.invoke(this, request, response);
			if (url != null) { //请求转发
				//把不同的请求转发出去，关键获得不同方法需要转发的url
				request.getRequestDispatcher(url).forward(request, response);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	public String execute(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		return null;
	}

}
